<template>
  <el-dialog
    v-model="dialogVisible"
    title="举报"
    width="500px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <!-- 举报内容预览 -->
    <div class="report-preview">
      <div v-if="content.image" class="preview-image">
        <el-image :src="content.image" :alt="content.title" fit="cover" />
      </div>
      <div class="preview-content">
        <h3 class="preview-title">{{ content.title }}</h3>
        <p v-if="content.description" class="preview-description">
          {{ content.description }}
        </p>
      </div>
    </div>

    <!-- 举报表单 -->
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="80px"
      class="report-form"
    >
      <!-- 举报类型 -->
      <el-form-item label="举报类型" prop="type">
        <el-radio-group v-model="form.type" class="report-types">
          <el-radio-button
            v-for="type in reportTypes"
            :key="type.value"
            :label="type.value"
          >
            {{ type.label }}
          </el-radio-button>
        </el-radio-group>
      </el-form-item>

      <!-- 举报原因 -->
      <el-form-item label="举报原因" prop="reason">
        <el-select
          v-model="form.reason"
          placeholder="请选择举报原因"
          class="w-full"
        >
          <el-option
            v-for="reason in getReasonsByType(form.type)"
            :key="reason.value"
            :label="reason.label"
            :value="reason.value"
          />
        </el-select>
      </el-form-item>

      <!-- 详细说明 -->
      <el-form-item label="详细说明" prop="description">
        <el-input
          v-model="form.description"
          type="textarea"
          :rows="4"
          placeholder="请详细描述问题（至少10个字符）"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>

      <!-- 图片证据 -->
      <el-form-item label="图片证据">
        <el-upload
          class="evidence-uploader"
          action="/api/upload"
          list-type="picture-card"
          :limit="3"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-upload="beforeUpload"
          :on-success="handleUploadSuccess"
        >
          <el-icon><Plus /></el-icon>
          <template #tip>
            <div class="upload-tip">
              支持jpg、png格式，最多3张，每张不超过2MB
            </div>
          </template>
        </el-upload>
      </el-form-item>

      <!-- 联系方式 -->
      <el-form-item label="联系方式" prop="contact">
        <el-input
          v-model="form.contact"
          placeholder="请留下您的联系方式（选填）"
        />
        <div class="contact-tip">如需了解处理结果，请留下您的联系方式</div>
      </el-form-item>
    </el-form>

    <!-- 图片预览对话框 -->
    <el-dialog v-model="previewVisible" width="800px" append-to-body>
      <img :src="previewUrl" alt="预览图片" class="w-full" />
    </el-dialog>

    <!-- 操作按钮 -->
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="loading" @click="handleSubmit">
          提交举报
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed } from "vue";
import { ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  content: {
    type: Object,
    required: true,
    default: () => ({
      title: "",
      description: "",
      image: "",
      type: "",
    }),
  },
});

const emit = defineEmits(["update:modelValue", "success"]);

// 对话框可见性
const dialogVisible = computed({
  get: () => props.modelValue,
  set: (val) => emit("update:modelValue", val),
});

// 表单ref
const formRef = ref(null);
const loading = ref(false);

// 图片预览
const previewVisible = ref(false);
const previewUrl = ref("");

// 表单数据
const form = ref({
  type: "",
  reason: "",
  description: "",
  images: [],
  contact: "",
});

// 举报类型
const reportTypes = [
  { label: "内容违规", value: "content" },
  { label: "账号违规", value: "account" },
  { label: "商品违规", value: "product" },
  { label: "其他问题", value: "other" },
];

// 举报原因
const reasonOptions = {
  content: [
    { label: "垃圾广告", value: "spam" },
    { label: "色情低俗", value: "porn" },
    { label: "暴力血腥", value: "violence" },
    { label: "政治敏感", value: "political" },
    { label: "侵犯权益", value: "rights" },
    { label: "虚假信息", value: "fake" },
  ],
  account: [
    { label: "冒充他人", value: "impersonate" },
    { label: "骚扰他人", value: "harassment" },
    { label: "垃圾账号", value: "spam_account" },
    { label: "违规营销", value: "marketing" },
  ],
  product: [
    { label: "商品违禁", value: "prohibited" },
    { label: "虚假宣传", value: "false_ad" },
    { label: "价格违规", value: "price" },
    { label: "假冒伪劣", value: "fake_product" },
  ],
  other: [{ label: "其他原因", value: "other" }],
};

// 表单校验规则
const rules = {
  type: [{ required: true, message: "请选择举报类型", trigger: "change" }],
  reason: [{ required: true, message: "请选择举报原因", trigger: "change" }],
  description: [
    { required: true, message: "请填写详细说明", trigger: "blur" },
    { min: 10, message: "详细说明不能少于10个字符", trigger: "blur" },
  ],
  contact: [
    {
      pattern: /^1[3-9]\d{9}$|^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
      message: "请输入正确的手机号或邮箱",
      trigger: "blur",
    },
  ],
};

// 根据类型获取原因选项
const getReasonsByType = (type) => {
  return reasonOptions[type] || [];
};

// 处理图片预览
const handlePreview = (file) => {
  previewUrl.value = file.url;
  previewVisible.value = true;
};

// 处理图片移除
const handleRemove = (file) => {
  const index = form.value.images.indexOf(file.url);
  if (index !== -1) {
    form.value.images.splice(index, 1);
  }
};

// 上传前校验
const beforeUpload = (file) => {
  const isImage = file.type.startsWith("image/");
  const isLt2M = file.size / 1024 / 1024 < 2;

  if (!isImage) {
    ElMessage.error("只能上传图片文件!");
    return false;
  }
  if (!isLt2M) {
    ElMessage.error("图片大小不能超过 2MB!");
    return false;
  }
  return true;
};

// 处理上传成功
const handleUploadSuccess = (response) => {
  form.value.images.push(response.url);
  ElMessage.success("上传成功");
};

// 提交举报
const handleSubmit = async () => {
  if (!formRef.value) return;

  try {
    await formRef.value.validate();
    loading.value = true;

    // TODO: 调用举报API
    await new Promise((resolve) => setTimeout(resolve, 1000));

    ElMessage.success("举报提交成功");
    emit("success", form.value);
    dialogVisible.value = false;
  } catch (error) {
    console.error("提交失败:", error);
    ElMessage.error("表单验证失败");
  } finally {
    loading.value = false;
  }
};

// 关闭对话框
const handleClose = () => {
  formRef.value?.resetFields();
  form.value = {
    type: "",
    reason: "",
    description: "",
    images: [],
    contact: "",
  };
  previewVisible.value = false;
  previewUrl.value = "";
};
</script>

<style lang="scss" scoped>
.report-preview {
  @apply mb-6 bg-gray-50 rounded-lg overflow-hidden;

  .preview-image {
    @apply w-full h-48;

    :deep(.el-image) {
      @apply w-full h-full;
    }
  }

  .preview-content {
    @apply p-4;

    .preview-title {
      @apply text-lg font-medium text-gray-900 mb-2;
    }

    .preview-description {
      @apply text-sm text-gray-600 line-clamp-2;
    }
  }
}

.report-form {
  .report-types {
    @apply w-full flex flex-wrap gap-2;

    :deep(.el-radio-button__inner) {
      @apply rounded-full;
    }
  }

  .evidence-uploader {
    :deep(.el-upload--picture-card) {
      @apply w-24 h-24;
    }
  }

  .upload-tip {
    @apply text-xs text-gray-500 mt-1;
  }

  .contact-tip {
    @apply text-xs text-gray-500 mt-1;
  }
}

.dialog-footer {
  @apply flex justify-end gap-2;
}
</style>
